/* DetailTableDataSource.m
 *
 * A data source implementation for a detail table.
 * 
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * 
 */



#import <appkit/appkit.h>
#import <eoaccess/eoaccess.h>
#import "DetailTableDataSource.h"
#import "TableDataSourcePrivate.h"

@implementation DetailTableDataSource

- initWithMasterDataSource:(TableDataSource *)masterSource entity:(EOEntity *)detailEntity {
	NSString *primaryKey;
	NSArray *relations;
	int i;
	NSMutableArray *qualifiers = [[[NSMutableArray alloc] init] autorelease];

	[super init];
	entity = [detailEntity retain];
	master = [masterSource retain];
	orderByKey=nil;
	
	primaryKey = [(EOAttribute *)[[entity primaryKeyAttributes] objectAtIndex:0] name];
	relations = [entity relationships];
	for (i=0; i<[relations count]; i++) {
		EORelationship *rel=[relations objectAtIndex:i];

		if ([rel isToMany]) {
			EOAttribute *attr = [[rel sourceAttributes] objectAtIndex:0];
			if (!attr) continue;
			else [qualifiers addObject:[attr name]];
		}
	}
	relations = [[master entity] relationships];
	for (i=0; i<[relations count]; i++) {
		EORelationship *rel=[relations objectAtIndex:i];

		if ([rel isToMany] && [entity isEqual:[rel destinationEntity]]) {
			EOAttribute *attr = [[rel destinationAttributes] objectAtIndex:0];
			if (!attr) continue;
			else [qualifiers addObject:[attr name]];
		}
	}

	return [self initWithTable:[master tablePath] primaryKey:primaryKey qualifierKeys:qualifiers];
}

- (void)qualifyWithRelationshipKey:key ofObject:sourceObject {

	EORelationship *rel = [[master entity] relationshipNamed:key];
	NSMutableDictionary *qual;
	EOAttribute *source = [[rel sourceAttributes] objectAtIndex:0];
	EOAttribute *dest = [[rel destinationAttributes] objectAtIndex:0];

	if (!rel) {
		NSLog(@"Master source does not have relationship for key %@", [(NSString *)key description]);
		[self setQualifier:nil];
	}
	else if (!source || !dest) {
		NSLog(@"Master source does not have source or destination attribute");
		[self setQualifier:nil];
	}
	else if (!sourceObject) {
		[self setQualifier:nil];
	}
	else {
		id linkValue;
		linkValue = [sourceObject objectForKey:[source name]];
		if (linkValue) {
			qual = [[[NSMutableDictionary alloc] init] autorelease];
			[qual setObject:[dest name] forKey:@QPROPERTY];
			[qual setObject:linkValue forKey:@QVALUE];
			[self setQualifier:qual];
		}
		else [self setQualifier:nil];
	}	
	/*[masterObject autorelease];
	masterObject=[sourceObject retain];
	[masterKey autorelease];
	masterKey=[key retain];*/
}

- createObject{
	NSMutableDictionary *newObject = [super createObject];
	NSArray *relations = [[master entity] relationships];
	int i;

	if (!newObject) return nil;
	for (i=0; i<[relations count]; i++) {
		EORelationship *rel=[relations objectAtIndex:i];

		if ( ([rel isToMany]) && ([entity isEqual:[rel destinationEntity]]) ) {
			EOAttribute *attr = [[rel destinationAttributes] objectAtIndex:0];
			NSString *key = [attr name];

			if (!attr) continue;
			if ( ![key isEqual:[qualifier objectForKey:@QPROPERTY]] ) {
				NSLog(@"qualifier %@ differs from relationship key %@ to create detail object %@", 
					[(NSMutableDictionary *)qualifier description], key, [entity name]);
				continue;
			}
			else [newObject setObject:[qualifier objectForKey:@QVALUE] forKey:key];
		}
	}


	return newObject;
}


@end
